package com.glory.hikvisionmobile.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.SortingListAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.TrackOut.TrackOutLoadBean;
import com.glory.hikvisionmobile.bean.TrackOut.TrackOutSubmitBean;
import com.glory.hikvisionmobile.bean.TrackOut.XSortingAndBoundsBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;


import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class Work_SortingTrackOutActivity extends BaseActivity {

    @BindView(R.id.tvLot)
    TextView tvLot;
    @BindView(R.id.tvMainWork)
    TextView tvMainWork;
    @BindView(R.id.tvLotNum)
    TextView tvLotNum;
    @BindView(R.id.tvCurrentStep)
    TextView tvCurrentStep;
    @BindView(R.id.etRemarks)
    FreeUI_ClearEditText etRemarks;
    @BindView(R.id.rvSortingList)
    RecyclerView rvSortingList;
    @BindView(R.id.bottom_tv1)
    TextView bottomTv1;
    @BindView(R.id.bottom_tv2)
    TextView bottomTv2;
    @BindView(R.id.bottom_tv3)
    TextView bottomTv3;
    @BindView(R.id.bottom_tv4)
    TextView bottomTv4;
    @BindView(R.id.bottom_btn_layout)
    LinearLayout bottomBtnLayout;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.list_tv5)
    TextView listTv5;
    @BindView(R.id.list_tv6)
    TextView listTv6;
    private String lotNum;
    private SortingListAdapter sortingListAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_sorting;
    }

    @Override
    protected void initView() {
        initList();
        Bundle bundle = getIntent().getExtras();
        lotNum = bundle.getString("lotNum");
        tvLot.setText(lotNum);

        rvSortingList.setLayoutManager(new LinearLayoutManager(Work_SortingTrackOutActivity.this));
        rvSortingList.setItemAnimator(new DefaultItemAnimator());
        rvSortingList.addItemDecoration(new DividerItemDecoration(Work_SortingTrackOutActivity.this, 1));
        sortingListAdapter = new SortingListAdapter(null);
        sortingListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        sortingListAdapter.isFirstOnly(true);
        rvSortingList.setAdapter(sortingListAdapter);
    }


    @Override
    protected void initData() {
        JsonObject map = new JsonObject();
        map.addProperty("lotId",lotNum);
        NetUtils.requestDara("HIK.PdaTrackOutLoad",this,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<TrackOutLoadBean>>(
                new TypeToken<BaseResponseBean<TrackOutLoadBean>>() {
                }.getType(), getSupportFragmentManager(), this){

            @Override
            public void onSuccess(Response<BaseResponseBean<TrackOutLoadBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())) {
                    TrackOutLoadBean trackOutLoadBean = response.body().getBody();
                    List<XSortingAndBoundsBean> xSortingAndBoundsBeans = trackOutLoadBean.getxDieSortings();
                    sortingListAdapter.setNewData(xSortingAndBoundsBeans);
                    tvCurrentStep.setText(trackOutLoadBean.getCurrentStepDesc());
                    tvMainWork.setText(trackOutLoadBean.getMainEquipments());
                    tvLotNum.setText(String.valueOf(trackOutLoadBean.getMainQty() ));

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<TrackOutLoadBean>> response) {
                super.onError(response);
                loadError(response.getException(),"HIK.PdaTrackOutLoad");

            }
        });


    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("分选待出站");
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick({R.id.bottom_tv1, R.id.bottom_tv2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bottom_tv1:
                initPermission("TrackOut", this, new ActionControlListener() {
                    @Override
                    public void onControlChangeListener() {
                        submit("Comment","HIK.PdaTrackOut");
                    }
                });
                break;
            case R.id.bottom_tv2:
                initPermission("TrackExit", this, new ActionControlListener() {
                    @Override
                    public void onControlChangeListener() {
                        if (FreeApi_StringUtils.isEmpty(etRemarks.getText().toString().trim())){
                            showToast("请输入备注!",false);
                            return;
                        }
                        submit("AbortLot","HIK.PdaTrackAbort");
                    }
                });
                break;
        }
    }

    private void submit(String actionType, String messageName) {
        DialogUtils.showLoginDialog(this, Constant.CURRENT_USER, new DialogUtils.signCallback() {
            @Override
            public void onSignSuccess() {
                JsonObject map = new JsonObject();
                map.addProperty("lotId",lotNum);
                map.addProperty("actionType","Check");
                map.addProperty("comment",etRemarks.getText().toString().trim());
                map.addProperty("operationType",actionType);
                map.addProperty("mainEquipmentId",tvMainWork.getText().toString());
                NetUtils.requestDara(messageName,this,map,new FreeUI_DialogEntityCallBack<BaseResponseBean<TrackOutSubmitBean>>(
                        new TypeToken<BaseResponseBean<TrackOutSubmitBean>>() {
                        }.getType(), getSupportFragmentManager(), this){

                    @Override
                    public void onSuccess(Response<BaseResponseBean<TrackOutSubmitBean>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())) {
                            if (actionType.equals("Comment")){
                                String nextStepName = response.body().getBody().getNextStepDesc();
                                if (FreeApi_StringUtils.isEmpty(nextStepName)){
                                    showConfirmDialog(Work_SortingTrackOutActivity.this,"出站成功，没有下一道工序");
                                }else {
                                    showConfirmDialog(Work_SortingTrackOutActivity.this,"出站成功，下一道工序是:" + nextStepName);
                                }

                            }else {
                                showConfirmDialog(Work_SortingTrackOutActivity.this,"退站成功");
                            }
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<TrackOutSubmitBean>> response) {
                        super.onError(response);
                        loadError(response.getException(),messageName);

                    }
                });

            }
        },getSupportFragmentManager());

    }

    private void initList() {
        bottomTv1.setText("出站");
        bottomTv2.setText("退站");
        bottomTv1.setTag("TrackOut");
        bottomTv2.setTag("TrackExit");
        bottomTv3.setVisibility(View.GONE);
        bottomTv4.setVisibility(View.GONE);

        listTv1.setText("序号");
        listTv2.setText("WaferID");
        listTv3.setText("X轴");
        listTv4.setText("Y轴");
        listTv5.setText("清洗/载具");
        listTv6.setText("位置");

        listTv1.setVisibility(View.VISIBLE);
        listTv2.setVisibility(View.VISIBLE);
        listTv3.setVisibility(View.VISIBLE);
        listTv4.setVisibility(View.VISIBLE);
        listTv5.setVisibility(View.VISIBLE);
        listTv6.setVisibility(View.VISIBLE);
    }
}
